/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Print;

import gui.usergui.ResultFrame;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author RATSUF
 */
public class ExperimentReportCreator implements Printable {

    ResultFrame rf;
    
    int lineHeight;
    int[] pageBreaks;
    String[] textLines;
    JPanel[] imgPage;
    int[] imgBreaks;
    int numBreaksForImg;
    int linesPrints = 0;
    int imgPerPage;
   

    public ExperimentReportCreator(ResultFrame rf) {
        this.rf = rf;
    }

//    private void initTextLines() {
//        if (textLines == null) {
//            int numLines = 180;
//            textLines = new String[numLines];
//            for (int i = 0; i < numLines; i++) {
//                textLines[i] = "This is line number " + i;
//            }
//        }
    private void initTextLines() {
        if (textLines == null) {
            int numLines = rf.getExp().getDataResult().getCountParams();
            textLines = new String[numLines];

            for (int i = 0; i < numLines; i++) {
                textLines[i] = rf.getExp().getDataResult().getParam(i).getName() + "= "
                        + Double.toString(rf.getExp().getDataResult().getParam(i)
                        .getValue());
            }
////           
        }
    }

    private void initImages() {
        if (imgPage == null) {
            int numImg = rf.getJp_graphs().getComponentCount();
            System.out.println("numImg= " + numImg);
            imgPage = new JPanel[numImg];
            for (int i = 0; i < numImg; i++) {
                imgPage[i] = ((JPanel) ((JPanel) rf.getJp_graphs().getComponent(i)).
                        getComponent(1));
            }

        }

    }
 
 Graphics g;
    @Override
    public int print(Graphics g, PageFormat pf, int pageIndex)
            throws PrinterException {
        
       
        if (pageIndex == 0) {
            initTextLines();
            initImages();
            Font font = new Font("Serif", Font.PLAIN, 14);
            FontMetrics metrics = g.getFontMetrics(font);
            lineHeight = metrics.getHeight();
        }
            
        
            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
//        if (pageIndex > ) {
//            return NO_SUCH_PAGE;
//        }
       

        

    
        System.out.println("Инициализация шрифта");


        System.out.println("Инициализация строк");

        if (imgBreaks == null) {
            initImages();
            System.out.println("Инициализация JPanel's");
            imgPerPage = (int) pf.getImageableHeight() / imgPage[0].getHeight();
            System.out.println("imgperpage= " + imgPerPage);
            numBreaksForImg = imgPage.length / imgPerPage;
            imgBreaks = new int[numBreaksForImg];
            for (int i = 0; i < numBreaksForImg; i++) {
                imgBreaks[i] = (i + 1) * imgPerPage;

            }

        }
        if (pageBreaks == null) {

            initTextLines();
            int numBreaks = 0;
            int linesPerPage = (int) (pf.getImageableHeight() / lineHeight);

            if (((textLines.length - 1) / linesPerPage) > 1) {
                numBreaks = ((textLines.length - 1) / linesPerPage);
                pageBreaks = new int[numBreaks];
                for (int b = 0; b < numBreaks; b++) {
                    pageBreaks[b] = (b + 1) * linesPerPage;
                }
            } else {
                pageBreaks = new int[1];
                pageBreaks[0] = textLines.length;
            }




        }

        double pages = imgPage.length * 0.24 + textLines.length * 0.03;
        System.out.println("pages= " + (int) pages);




        if (pageIndex > (int) pages) {
            return NO_SUCH_PAGE;
        }





       
         int height = 0;

        int startImg =
                (pageIndex == 0) ? 0 : imgBreaks[pageIndex - 1];

        int endImg =
                (pageIndex == imgBreaks.length)
                ? imgPage.length : imgBreaks[pageIndex];
          
        for (int i = startImg; i < endImg; i++) {

            g2d.drawImage(canvasToImage(imgPage[i]), 0, height,
                    (int) pf.getImageableWidth() + 50,
                    imgPage[i].getHeight(), null);
            height += imgPage[i].getHeight();


        }
//        
        int start;
        if (pageBreaks.length > 1) {
            start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1];
        } else {
            if (linesPrints > 0) {
                 start = linesPrints;
            } else {
                start = linesPrints +1;
            }
           
        }

        int y = height + 10;
        int end;
        if (pageBreaks.length > 1) {
            end = (pageIndex == pageBreaks.length)
                    ? linesPrints += (int) ((pf.getImageableHeight() - imgPage[0].getHeight() * imgPerPage) / lineHeight)
                    : pageBreaks[pageIndex];
        } else {
            end = linesPrints += (((int) (pf.getImageableHeight() - y))/ lineHeight);
//                    imgPage[0].getHeight() * imgPerPage)-10) / lineHeight);
        }

        System.out.println("imgBreaks= " + imgBreaks.length);
        System.out.println("lineprints= " + linesPrints);

       
      



       
        for (int line = start; line < end; line++) {

            y += lineHeight;
            g2d.drawString(textLines[line], 0, y);

        }
        
        return PAGE_EXISTS;
    }
    
    public BufferedImage canvasToImage(Component cnvs) {

        int w = cnvs.getWidth();
        int h = cnvs.getHeight();
//        System.out.println("wb= " + w);
//        System.out.println("hb= " + h);

        int type = BufferedImage.SCALE_FAST;

        BufferedImage image = new BufferedImage(w, h, type);
        Graphics2D g2 = image.createGraphics();
        cnvs.paint(g2);
        g2.dispose();
        return image;
    }
}
